package com.thh.excel.controller;


import com.thh.excel.util.DownloadUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/excel")
public class OutProductController extends BaseController {





    /**
     * 出货表导出(1)普通导出
     */
    @RequestMapping("/printExcel")
    public void printExcel() throws IOException {
        // 第一部分：导出第一行
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        // 设置列宽
        sheet.setColumnWidth(0, 256 * 5);
        sheet.setColumnWidth(1, 256 * 26);
        sheet.setColumnWidth(2, 256 * 11);
        sheet.setColumnWidth(3, 256 * 29);
        sheet.setColumnWidth(4, 256 * 15);
        sheet.setColumnWidth(5, 256 * 10);
        sheet.setColumnWidth(6, 256 * 10);
        sheet.setColumnWidth(7, 256 * 10);
        sheet.setColumnWidth(8, 256 * 10);

        // 合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 8));

        Row row = sheet.createRow(0);
        row.setHeightInPoints(36);
        Cell cell = row.createCell(1);
        //2012年8月份出货表     2019-09
     //   String title = inputDate.replace("-0", "-").replace("-", "年") + "月份出货表";
      //  cell.setCellValue(title);
        // 设置样式
        cell.setCellStyle(this.bigTitle(workbook));


        // 第二部分：导出第二行
        String[] header = {"客户", "订单号", "货号", "数量", "工厂", "工厂交期", "船期", "贸易条款"};
        row = sheet.createRow(1);
        row.setHeightInPoints(26.3f);
        for (int i = 0; i < header.length; i++) {
            cell = row.createCell(i + 1);
            cell.setCellValue(header[i]);
            cell.setCellStyle(this.title(workbook));
        }

        // 第三部分：导出数据行
        List<Map> dataMapList = new ArrayList<>();

        Map dbData = new HashMap();

        dbData.put("customName","客户名称");
        dbData.put("contractNo","合同编号");
        dbData.put("productNo","生产编号");
        dbData.put("number",2);
        dbData.put("factoryName","工厂名称");
        dbData.put("deliveryPeriod","deliveryPeriod");
        dbData.put("shipTime",new Date());
        dbData.put("tradeTerms","tradeTerms");
        dataMapList.add(dbData);
        if (dataMapList != null && dataMapList.size() > 0) {
            int index = 2;
            for (Map data : dataMapList) {
                row = sheet.createRow(index++);
                row.setHeightInPoints(24);

                cell = row.createCell(1);
                cell.setCellValue((String)data.get("customName"));
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(2);
                cell.setCellValue((String)data.get("contractNo"));
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(3);
                cell.setCellValue((String)data.get("productNo"));
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(4);
                if (data.get("number") != null) {
                    cell.setCellValue((Integer) data.get("number"));
                }
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(5);
                cell.setCellValue((String) data.get("factoryName"));
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(6);
                cell.setCellValue((String) data.get("deliveryPeriod"));
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(7);
                cell.setCellValue((Date)data.get("shipTime"));
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(8);
                cell.setCellValue((String) data.get("tradeTerms"));
                cell.setCellStyle(this.text(workbook));
            }
        }

        // 第四部分：导出下载
        DownloadUtil downloadUtil = new DownloadUtil();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //excel文件流--->bos----->response
        workbook.write(bos);
        downloadUtil.download(bos, response, "报表.xlsx");
        bos.close();
    }


    //大标题的样式
    public CellStyle bigTitle(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        style.setBorderTop(BorderStyle.THIN);                        //上细线
        style.setBorderBottom(BorderStyle.THIN);                    //下细线
        style.setBorderLeft(BorderStyle.THIN);                        //左细线
        style.setBorderRight(BorderStyle.THIN);                        //右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short) 10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);                //横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        style.setBorderTop(BorderStyle.THIN);                        //上细线
        style.setBorderBottom(BorderStyle.THIN);                    //下细线
        style.setBorderLeft(BorderStyle.THIN);                        //左细线
        style.setBorderRight(BorderStyle.THIN);                        //右细线

        return style;
    }
}